package com.wing.product.controller.app;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.ProductCategory;
import com.wing.product.model.entity.ProductDetail;
import com.wing.product.model.request.ProductDetailForm;
import com.wing.product.model.response.ProductDetailVO;
import com.wing.product.model.response.ProductVO;
import com.wing.product.service.ProductCategoryService;
import com.wing.product.service.ProductDetailService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-产品明细表管理", tags = "#/product/productDetail")
@RestController("appProductDetailController")
@RequestMapping("productCab/app/productDetail")
@RequiredArgsConstructor
public class ProductDetailController {

    private final ProductDetailService productDetailService;

    @Autowired
    private ProductCategoryService categoryService;



    @ApiOperation(value = "查询所有产品SKU表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ProductDetailVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "old_spec_code") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String skuCode
            ,Long productTypeId
            ,Long productCategoryId
            ,Long distributionDemandId
            ,String name
            ,String belongCabinet
            ,Long workOrderId
            , ProductCategory.CategoryType categoryType
    ){

        Page<ProductDetail> pg = new Page<>(page, limit);

        //排序
        Map paramMap=new HashMap();
        paramMap.put("sort",StrUtil.toUnderlineCase(sort));
        paramMap.put("order",StrUtil.toUnderlineCase(order));
        paramMap.put("skuCode",skuCode);
        paramMap.put("productTypeId",productTypeId);
        paramMap.put("productCategoryId",productCategoryId);
        paramMap.put("distributionDemandId",distributionDemandId);
        paramMap.put("name",name);
        paramMap.put("belongCabinet",belongCabinet);
        paramMap.put("workOrderId",workOrderId);
        if(null != categoryType){
            paramMap.put("categoryType",categoryType.getValue());
        }
        //搜索

        IPage<ProductDetailVO> list = productDetailService.pageList4app(pg,paramMap);
        if (list.getRecords().size()==0){
            paramMap.remove("workOrderId");
            list = productDetailService.pageList4app(pg,paramMap);
        }
        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "添加产品明细表", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(ProductDetailForm.Add.class) ProductDetailForm productDetailForm) {
        if (productDetailService.add(productDetailForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }




    @ApiOperation(value = "修改产品明细表", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(ProductDetailForm.Update.class) ProductDetailForm productDetailForm) {
        ProductDetail productDetail = productDetailService.getById(productDetailForm.getId());
        if(productDetail == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ProductDetail.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(productDetailForm, productDetail, copyOptions);
        if (productDetailService.updateById(productDetail)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



@ApiOperation(value = "删除产品明细表", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productDetailId", value = "产品明细表id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long productDetailId) {
        if (productDetailService.removeById(productDetailId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
    @ApiOperation(value = "查询所有产品", notes = "查询")
    @GetMapping("/getAll")
    public JsonResult<ProductDetail> getAll(){
        QueryWrapper<ProductDetail> wrapper = new QueryWrapper<>();
        wrapper.select("sku_code","sku_name");
        List<ProductDetail> productDetail = productDetailService.list(wrapper);
        return JsonResult.success(productDetail);
    }

    /**
     * 提供给签收刀具入库类型查询产品使用
     * @param skuCode
     * @return
     */
    @ApiOperation(value = "查询产品相关信息", notes = "查询")
    @GetMapping("/getProductInfoBySkuCode")
    public JsonResult<ProductDetailVO> getProductInfoBySkuCode(@RequestParam("skuCode") String skuCode){
        ProductDetailVO info = productDetailService.getProductInfoBySkuCode(skuCode);
        return JsonResult.success(info);
    }

    /**
     * 产品包信息
     */
    @GetMapping("/productList")
    public PageResult<ProductVO> productList(
             @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order){
        Page<ProductDetail> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();
        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        //搜索
        IPage<ProductVO> list = productDetailService.productList(pg,params);
        return new PageResult(list.getRecords(), list.getTotal());
    }

    @GetMapping("/updatePicBySkuCode")
    public JsonResult updatePicBySkuCode(String skuCode,String picUrl){
        ProductDetail productDetail=new ProductDetail();
        productDetail.setProductPic(picUrl);
        productDetailService.update(productDetail,new QueryWrapper<ProductDetail>().eq("sku_code",skuCode));
        return JsonResult.success("修改成功");
    }
}
